package mi.payment.utils.transaction;


import android.content.Context;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import mi.payment.settings.QPSParamsOperation;
import mi.payment.utils.iso8583.MsgField;
import mi.payment.utils.iso8583.Utils;
import sunmi.sunmiui.utils.LogUtil;

/**
 * 卡 BIN 下载
 * Created by yf on 2017/7/25.
 */

public class CardBinDownloadTask extends TransactionTask<Void, Void, String> {

    private static final String TAG = "CardBinDownloadTask";

    private String[] Data60_3;

    public CardBinDownloadTask(Context c) {
        super(c, false);
    }

    public void initBinAParams() {

    }

    public void initBinBParams() {
        set60_3Datas("396", "397");
    }

    //黑名单
    public void initBinCParams() {
        set60_3Datas("398", "399");
    }

    /**
     * 下载卡BIN时60.3域的数据
     *
     * @param start 下载卡BIN
     * @param over  卡BIN结束下载
     */
    private void set60_3Datas(String start, String over) {
        Data60_3 = new String[2];
        Data60_3[0] = start;
        Data60_3[1] = over;
    }

    @Override
    protected void onBegin(Void[] params) {

    }

    @Override
    protected String appointResult() {
        return resultStr;
    }

    @Override
    protected Map<String, MsgField> preAssembly() {
        return null;
    }


    @Override
    protected byte[] changeAndAssembly(Map<String, MsgField> fieldMap) {
        return null;
    }

    @Override
    protected String doTransactionSelf() {
        CardBinParamsDownloadTask cardBinDown = new CardBinParamsDownloadTask(context);
        DownloadFinishedTask cardBinDownFiish = new DownloadFinishedTask(context);

        resultStr = "FF";

        // ----       获取并保存卡BIN        ------//
        String ret;
        String tempStr = "000";
        List<String> binList = new ArrayList<>();
        while (true) {
            String[] paramsDownload = new String[]{Data60_3[0], tempStr};
            ret = cardBinDown.syncExecute(paramsDownload);
            binList.add(Utils.hexStr2AsciiStr(ret));
            LogUtil.e(TAG, Utils.hexStr2AsciiStr(ret));
            String t = Utils.hexStr2AsciiStr(ret).substring(0, 1);
//            LogUtil.e(TAG, "返回报文标识： " + t);
            if (t.equals("2")) {
                tempStr = Utils.hexStr2AsciiStr(ret).substring(1, 4);
//                LogUtil.e(TAG, "下载卡BIN的总数量" + tempStr);
                int pushCount = Integer.valueOf(tempStr) + 1;
                tempStr = String.format("%03d", pushCount);
//                LogUtil.e(TAG, "上送的数量" + tempStr);
            } else {
                break;
            }
        }
        saveCardBin(binList);

        resultStr = cardBinDownFiish.syncExecute(Data60_3[1], "0800");
        //添加处理要求
        if (cardBinDownFiish.getProcessCode() != -1) {
            ProcessRequireManager.syncCheckProcessCode(cardBinDownFiish.getProcessCode());
        }

        return resultStr;
    }

    private void saveCardBin(List<String> cardBinList) {
        if (cardBinList.size() == 0) {
            return;
        }
        int count = 0;

        if (Data60_3[0].equals("396")) {
            String cardBinStr = "";
            for (String cardBin : cardBinList) {
                if (cardBin.length() < 12)
                    return;
                if ((cardBin.length() - 4) % 8 == 0) {
                    count += (cardBin.length() - 4) / 8;
                }
                cardBinStr += cardBin.substring(4, cardBin.length());
                LogUtil.e(TAG, "-BinB----" + cardBin.substring(4, cardBin.length()));
            }

            QPSParamsOperation.getInstance().saveCardBinB(cardBinStr);
            LogUtil.e(TAG, "保存的卡BINB   " + cardBinStr + "   保存卡BINB的数量 " + count);

        } else if (Data60_3[0].equals("398")) {
            String cardBinStr = "";
            for (String cardBin : cardBinList) {
                if (cardBin.length() < 12)
                    return;
                if ((cardBin.length() - 4) % 8 == 0) {
                    count += (cardBin.length() - 4) / 8;
                }
                cardBinStr += cardBin.substring(4, cardBin.length());
                LogUtil.e(TAG, "-BinC----" + cardBin.substring(4, cardBin.length()));
            }
            QPSParamsOperation.getInstance().saveCardBinC(cardBinStr);
            LogUtil.e(TAG, "保存的卡BINC（黑名单）   " + cardBinStr + "   保存卡BINC（黑名单）的数量 " + count);
        }

    }
}
